package gvo.basicDatatypes;

import gvo.presets.Presets;
import java.io.Serializable;


public class AutorenDat implements Serializable {
    private static final long serialVersionUID = 1L;
    
    private int idAutor;
    private String name;
    private String nachname;
    private String vorname;
    private String zusatz;
    private String notiz;
    public boolean isVerified = false;
	public boolean isInDB = false;
	public boolean multiple = false;
    public static final String XMLNAME = "AutorenDat";
	public static final String[] Titel={"idAutor","name","nachname", "vorname", "zusatz", "notiz"};
	public static final int IDAutor = 0;
	public static final int NAME = 1;
	public static final int NACHNAME = 2;
	public static final int VORNAME = 3;
	public static final int ZUSATZ = 4;
	public static final int NOTIZ = 5;
	public AutorenDat() {
		this.idAutor = Presets.NOID;
		this.name = Presets.EMPTYTXT;
		this.nachname = Presets.EMPTYTXT;
		this.vorname = Presets.EMPTYTXT;
		this.zusatz = Presets.EMPTYTXT;
		this.notiz = Presets.EMPTYTXT;
    }
	public AutorenDat(int idAutor, String name,  String nachname, String vorname, String zusatz, String notiz, boolean isVerified, boolean isInDB){
		this.idAutor = idAutor;
		this.name = name;
		this.nachname = nachname;
		this.vorname = vorname;
		this.zusatz = zusatz;
		this.notiz = notiz;
		this.isVerified = isVerified;
		this.isInDB = isInDB;
		splitName(false);
	}
    public AutorenDat(String name) {
    	this.idAutor = Presets.NOID;
    	this.name = name;
    	this.nachname = Presets.EMPTYTXT;
		this.vorname = Presets.EMPTYTXT;
		this.zusatz = Presets.EMPTYTXT;
		this.notiz = Presets.EMPTYTXT;
		splitName(false);
    }
    public String info(){
		return String.format("AUTORENDAT %5d | %20.20s | %10.10s | %10.10s | %5.5s", 
				idAutor, name, nachname , vorname, zusatz);
	}
	public static String infoTitel(){
		return String.format("AUTOR      %5.5s | %20.20s | %10.10s | %10.10s | %5.5s", (Object[])Titel);
	}
	public String showAll(){
		return ">"+name+"< >"+nachname+"< >"+vorname+"<";
	}
    public String getNachname() {
		return nachname;
	}
	public void setNachname(String nachname) {
		this.nachname = nachname;
	}
	public String getVorname() {
		return vorname;
	}
	public void setVorname(String vorname) {
		this.vorname = vorname;
	}   
    public Integer getIdAutor() {
        return idAutor;
    }

    public void setIdAutor(Integer idAutor) {
        this.idAutor = idAutor;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        splitName(false);
    }
    public String getZusatz() {
		return zusatz;
	}

	public void setZusatz(String zusatz) {
		this.zusatz = zusatz;
	}
    public String getNotiz() {
		return notiz;
	}
	public void setNotiz(String notiz) {
		this.notiz = notiz;
	}
	public void splitName(boolean forceSplit){
		if(name==null){
			return;
		}
		if (name.equals(Presets.EMPTYTXT)){
			return;
		}
		if(vorname==null){
			vorname = Presets.EMPTYTXT;
		}
		if(nachname==null){
			nachname = Presets.EMPTYTXT;
		}
		if((vorname.equals(Presets.EMPTYTXT) && nachname.equals(Presets.EMPTYTXT))|| forceSplit){
			int ind = name.indexOf(",");
			if(ind==-1){
				nachname = name;
			} else {
				vorname = name.substring(ind+2);
				nachname = name.substring(0, ind);
			}
		}
	}
	public boolean equals(Object object) {
        if (!(object instanceof AutorenDat)) {
            return false;
        }
        AutorenDat other = (AutorenDat) object;
        
        if (this.idAutor == Presets.NOID){
     	   return false;
        }
        if(this.idAutor == other.idAutor){
     	   return true;
        }
        return false;
    }
	@Override
	public int hashCode() {
		int hash = 7;
		hash = 31 * hash + idAutor;
		hash = 31 * hash + (null == name ? 0 : name.hashCode());
		return hash;
	}
    public String prepName(){
    	
    	if (name == null){
    		return "<leerer Autor>";
    	}
    	if (name.equals(Presets.EMPTYTXT)){
    		return "<leerer Autor>";
    	}
    	if(!zusatz.equals(Presets.EMPTYTXT)){
    		
    		if(name.length()>Presets.MAXLENGTH){
    			if(zusatz.length() > 6){
    				return name.substring(0, Presets.MAXLENGTH-Presets.ZUSATZLENGTH) + " ("+zusatz.substring(0, Presets.ZUSATZLENGTH)+")";
        		} else {
        			return name.substring(0, Presets.MAXLENGTH-Presets.ZUSATZLENGTH)  +" ("+zusatz+")";
        		}
    		} else {
    			if(zusatz.length() + name.length()>Presets.MAXLENGTH){
        			return name + " ("+zusatz.substring(0, Presets.ZUSATZLENGTH)+")";
        		} else {
        			return name +" ("+zusatz+")";
        		}
    		}
    		
    	} else {
    		if(name.length()>Presets.MAXLENGTH){
        		return name.substring(0, Presets.MAXLENGTH);
        	} 
    	}
    	return name;
    }
    public String toString() {
    	return prepName();
    }

}

